// pages/edit_branch/edit_branch.js
var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isdisabled: false, //设置选择时间的picker框是否可用
    longitude: '',
    latitude: '',
    loadgroupmid: '',
    loadgroupid: '',
    loadname: '',
    districtid: '',
    districtmid: '',
    districtname: '',

    auto: false,
    branchname: '',
    longitude: '',
    latitude: '',
    controllerno: '',
    poweron_time: '00:01',
    poweroff_time: '23:59',
    maxelec: 0,
    minelec: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;

    let header = {
      'content-type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    }

    that.setData({
      districtid: options.districtid,
      districtmid: options.districtmid,
      loadgroupid: options.loadgroupid,
      loadgroupmid: options.loadgroupmid,
      branchid: options.branchid,
      branchmid: options.branchmid
    });

    wx.request({
      url: 'https://119.3.46.145/server/FindBranch?id=' + that.data.branchmid,
      method: 'POST',
      header: header,
      success: res => {  
        //{"data":"{\"auto\":\"1\",\"MaxCurrent\":\"0.0\",\"turnOfftime\":\"7:0:00\",\"devNo\":\"test_branch5\",\"UserId\":\"922aeb81-29c8-41ba-9f82-36d9abb33265\",\"groupID\":\"1\",\"MinCurrent\":\"0.0\",\"Latitude\":\"32.37159\",\"id\":\"76\",\"Longitude\":\"119.39384\",\"turnOntime\":\"17:29:00\",\"Name\":\"支路5\"}"}
        let result = JSON.parse(res.data.data);       

        that.setData({
          auto: result.auto?true:false,
          branchname: result.Name,
          longitude: result.Longitude,
          latitude: result.Latitude,
          controllerno: result.devNo,
          poweron_time: result.turnOntime,
          poweroff_time: result.turnOfftime,
          maxelec: result.MaxCurrent,
          minelec: result.MinCurrent        
        });

        //console.log(that.data, "that.data");
      },
      fail: function (error) {
        //console.log(error, "FindSingleLight error");
        wx.showModal({
          title: '提示',
          content: error
        });
      }
    })

  },

  choose_tude: function () {
    let that = this;

    wx.chooseLocation({
      success(res_location) {
        that.setData({
          longitude: res_location.longitude,
          latitude: res_location.latitude,
          //name: res_location.name
        });
      }
    })

  },


  formSubmit: function (e) {
    let that = this;
    //console.log(e.detail.value, "e");

    let header = {
      'Content-Type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    }

    let long_lati = e.detail.value.input_long_lati;
    let long_lati_arr = long_lati.split(",");

    if (e.detail.value.input_branchname == "") {
      wx.showModal({
        title: '提示',
        content: '支路名称不能为空',
        showCancel: false
      });

      return;
    }

    if (e.detail.value.controllerno == "") {
      wx.showModal({
        title: '提示',
        content: '控制器编号不能为空',
        showCancel: false
      });

      return;
    }

    if (long_lati_arr[0] == "" || long_lati_arr[1] == "") {
      wx.showModal({
        title: '提示',
        content: '经度或者纬度不能为空',
        showCancel: false
      });

      return;
    }

    that.setData({
      branchname: e.detail.value.input_branchname,
      controllerno: e.detail.value.input_controllerno,
      poweron_time: e.detail.value.picker_poweron_time,
      poweroff_time: e.detail.value.picker_poweroff_time,
      maxelec: e.detail.value.input_maxelec,
      minelec: e.detail.value.input_minelec,
      longitude: long_lati_arr[0],
      latitude: long_lati_arr[1],
      auto: e.detail.value.switch
    });

    wx.request({
      url: 'https://119.3.46.145/server/UpdateBranch',
      method: 'Post',    
      data: {
        userid: app.globalData.userid,
        devNo: that.data.controllerno,
        //devNo:'101101101101',
        Name: that.data.branchname,
        auto: that.data.auto,
        id: that.data.branchmid,
        ontime: e.detail.value.picker_poweron_time,
        offtime: e.detail.value.picker_poweroff_time,
        MaxCurrent: e.detail.value.input_maxelec,
        MinCurrent: e.detail.value.input_minelec,

        Longitude: that.data.longitude,
        Latitude: that.data.latitude
      },
      header: header,
      success: res => {
        //data: { error: "0", info: "添加成功" }  {error: "-1", info: "已存在，无法添加"}        
        let result = res.data;

        if (result.error == "0") {
          wx.showModal({
            title: '提示',
            content: result.info,
            success: function () {
              wx.navigateBack();
            }
            // success: function () {
            //   wx.request({
            //     url: 'https://119.3.46.145/server/loadTree?userid=' + app.globalData.userid + '&id=' + that.data.loadgroupid,  //获取路段包含的单灯及支路列表构造树
            //     header: header,
            //     success: function (res) {
            //       let data = JSON.parse(res.data.data);

            //       console.log(data, "data");

            //       if (data.length == 0) {
            //         return;
            //       }

            //       let tmp_branch_list = [];

            //       data.forEach(function (item, index, self) {
            //         if (item.attributes.devType == 1) {
            //           tmp_branch_list.push(item);
            //         }
            //       });

            //       let pages = getCurrentPages(); //获取上一个页面信息栈(a页面)
            //       let prevPage = pages[pages.length - 2] //给上一页面的longitude latitude赋值
            //       prevPage.setData({
            //         branch_list: tmp_branch_list
            //       });
            //       wx.navigateBack(); //关闭当前页面，返回上一个页面
            //     },
            //     fail: function (error) {
            //       console.log(error, "dev_manager  onload loadTree");
            //     }
            //   });
            // }
          });
        }

        if (result.error == "-1") {
          wx.showModal({
            title: '提示',
            content: result.info,
            showCancel: false,
            success:function(){
              wx.navigateBack();
            }
          })
        }
      },
      fail: function (error) {
        console.log(error, "addDepartment error");
      }
    })
  },

  formReset: function (e) {
    let that = this;
    // that.setData({
    //   loadgroupName: '',
    //   describe: '',
    //   name: '',
    //   telephone: '',
    //   longitude: '',
    //   latitude: '',
    // });
  },

  set_auto: function (e) {
    let that = this;
    //console.log(e,"e set_auto");
    if (e.detail.value === true) {
      that.setData({
        isdisabled: true
      });
    }

    if (e.detail.value === false) {
      that.setData({
        isdisabled: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})